package com.color.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.WebRequest;

import com.alibaba.fastjson.JSON;
import com.color.dao.IOpenNoDao;
import com.color.entity.OpenNo;
import com.color.service.IOpenNoService;
import com.color.utils.ObjectUtils;

@Service("openNoService")
public class OpenNoServiceImpl implements IOpenNoService{
	
	@Autowired
	@Qualifier("openNoDao")
	private IOpenNoDao openNoDao;

	public Map<String,Object> getPageList(ModelMap model,WebRequest request){
		
		String page = request.getParameter("page");
		String rows = request.getParameter("rows");
		
		List<OpenNo> list = openNoDao.getPageList(ObjectUtils.stringToInt(page), ObjectUtils.stringToInt(rows));
		int total = openNoDao.getCountList();
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("rows", list);
		map.put("total", total);
		return map;
	}
	
	public Map<String,Object> commitData(WebRequest request){
		
		String deleted = request.getParameter("deleted");
		String inserted = request.getParameter("inserted");
		String updated = request.getParameter("updated");
		
		if(deleted != null){
			//把json字符串转换成对象
		    List<OpenNo> listDeleted = JSON.parseArray(deleted, OpenNo.class);
		    for(OpenNo ser : listDeleted){
		    	openNoDao.Delete(ser);
		    }
		}
		if(inserted != null){
		    //把json字符串转换成对象
		    List<OpenNo> listInserted = JSON.parseArray(inserted, OpenNo.class);
		    for(OpenNo ser : listInserted){
		    	openNoDao.Save(ser);
		    }
		}
		if(updated != null){
		    //把json字符串转换成对象
		    List<OpenNo> listUpdated = JSON.parseArray(updated, OpenNo.class);
		    for(OpenNo ser : listUpdated){
		    	openNoDao.Save(ser);
		    }
		}
		
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("iRet", "0");
		map.put("strMsg", "操作成功!");
		return map;
	}
}
